package cn.itcast.service;

import cn.itcast.dao.RoleMapper;
import cn.itcast.domain.Role;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 查询所有角色
     *
     * @return
     */
    @Override
    public List<Role> findAllRoles() {
        return roleMapper.findAllRoles();
    }


    /**
     * 新增角色 并为角色设定权限 ,设定菜单
     *
     * @param role
     */
    @Override
    public void insertRole(Role role, Integer[] permissionIds, Integer[] menuIds) {

        roleMapper.insertRole(role);
        //获取新增的roleId
        Integer roleId = role.getId();
        //建立关联关系 :建立两次

        this.setRoleAndPermission(roleId, permissionIds);

        this.setRoleAndMenu(roleId, menuIds);


    }

    /**
     * 分页查询 条件查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findRoleByPage(QueryPageBean queryPageBean) {

        //健壮性判断
        Integer currentPage = queryPageBean.getCurrentPage();
        if (currentPage == null) {
            currentPage = 1;
        }
        Integer pageSize = queryPageBean.getPageSize();
        if (pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(currentPage, pageSize);

        Page<Role> page = (Page<Role>) roleMapper.findRoleByPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }


    /**
     * 删除角色
     *
     * @param id
     */
    @Override
    public void deleteRole(Integer id) {
        //判断是否已经关联 先查询
        //先删除三次 中间表 的RoleId
        roleMapper.deleteRoleMenu(id);
        roleMapper.deleteRolePermission(id);
        roleMapper.deleteUserRole(id);
        //再删除 自己表中的id
        roleMapper.deleteRole(id);
    }

    /**
     * 查询单个角色 信息回写
     *
     * @param id
     * @return
     */
    @Override
    public Role findRoleById(Integer id) {

        return roleMapper.findRoleById(id);
    }

    /* *
     * 修改角色
     *
     * @param role
     */
    @Override
    public void updateRole(Role role, Integer[] permissionIds, Integer[] menuIds) {


        //获取role 的id
        Integer roleId = role.getId();
        //删除中间表
        roleMapper.deleteRolePermission(roleId);
        roleMapper.deleteRoleMenu(roleId);
        //重建关联关系
        //修改
        roleMapper.updateRole(role);
        this.setRoleAndPermission(roleId, permissionIds);
        this.setRoleAndMenu(roleId, menuIds);

    }

    /**
     * 权限ids
     *
     * @param id
     * @return
     */
    @Override
    public List<Integer> findPermissionIds(Integer id) {
        return roleMapper.findPermissionIds(id);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public List<Integer> findMenuIds(Integer id) {
        return roleMapper.findMenuIds(id);
    }


    /*
     * 建立角色和权限的联系
     *
     * @param UserId
     * @param roleId
     */
    public void setRoleAndPermission(Integer roleId, Integer[] permissionIds) {
        List<Map<String, Object>> list = new ArrayList();
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map<String, Object> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("permissionId", permissionId);
                list.add(map);
            }
            //只有当checkItemIds不为空时,才会执行这个方法
            roleMapper.setRoleAndPermission(list);
        }
    }

    /*
     * 建立角色和菜单的联系
     *
     * @param UserId
     * @param roleId
     */
    public void setRoleAndMenu(Integer roleId, Integer[] menuIds) {
        List<Map<String, Object>> list = new ArrayList();
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map<String, Object> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("menuId", menuId);
                list.add(map);
            }
            //只有当checkItemIds不为空时,才会执行这个方法
            roleMapper.setRoleAndMenu(list);
        }
    }
}
